package com.ruoyi.swla2.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.swla2.domain.SysOss;
import com.ruoyi.swla2.mapper.PgEditMapper;
import com.ruoyi.swla2.service.IGisPointService;
import com.ruoyi.swla2.service.ISysOssService;
import com.ruoyi.swla2.util.MinioTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.Date;
import java.util.UUID;

@Service
public class SysOssServiceImpl implements ISysOssService {

    @Autowired
    private MinioTemplate minioTemplate;

    @Autowired
    PgEditMapper pgEditMapper;

    /**
     * 返回当前日期目录
     *
     * @return 格式如：2024/08/09/
     */
    private String getDateFolder() {
        LocalDate now = LocalDate.now();
        return now.getYear() + "/" + StrUtil.padPre(String.valueOf(now.getMonthValue()), 2, "0") + "/" + StrUtil.padPre(String.valueOf(now.getDayOfMonth()), 2, "0") + "/";
    }

    @Override
    public SysOss upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String finalFileName = this.getDateFolder() + uuid + suffix;
        minioTemplate.putObject(finalFileName, file);

        // 保存文件信息
        SysOss sysOss = new SysOss();
        sysOss.setFileName(finalFileName);
        sysOss.setOriginalName(originalFilename);
        sysOss.setFileSuffix(suffix);
        sysOss.setUrl(minioTemplate.getObjectUrl(finalFileName));

        return sysOss;
    }

    @Override
    public void download(String fileName, String originalName, HttpServletResponse response) throws IOException {
        FileUtils.setAttachmentResponseHeader(response, originalName);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
        try (InputStream inputStream = minioTemplate.getObjectContent(fileName)) {
            int available = inputStream.available();
            IoUtil.copy(inputStream, response.getOutputStream(), available);
            response.setContentLength(available);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public int remove(SysOss sysOss) {
        boolean removed = minioTemplate.removeObject(sysOss.getFileName());
        boolean del = pgEditMapper.deleteTableData("sys_oss", sysOss.getId());

        return removed && del ? 1 : 0;
    }
}
